<?php

namespace app\api\model\v1;

use think\Model;
use think\Db;

class Shopuser extends Model
{


    

    // 表名
    protected $name = 'wanlshop_shop';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';



    /**
     * 变更会员余额
     * @param int    $money   余额
     * @param int    $user_id 会员ID
     * @param string $memo    备注
     */
    public static function money($type,$money, $shop_id, $memo)
    {
        Db::startTrans();
        try {
            $shopuser = self::lock(true)->find($shop_id);
          
            if ($shopuser && $money != 0) {
                $before = $shopuser->money;
                //$after = $user->money + $money;
                $after = function_exists('bcadd') ? bcadd($shopuser->money, $money, 2) : $shopuser->money + $money;
                //更新会员信息
                $shopuser->save(['money' => $after]);
                if ($type == 1){
                    $content = "提现{$money}元";
                }elseif($type == 2){
                    $content = "提现被驳回";
                }
                //写入日志
                ShopMoneyLog::create(['shop_id' => $shop_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => $memo,'type'=>$type,'content'=>$content]);
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
    }


}

